import paramiko
import tkinter as tk
from tkinter import scrolledtext, messagebox
import threading

# 创建 SSH 客户端
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# 连接到远程服务器
def connect_ssh():
    hostname = host_entry.get()
    port = int(port_entry.get())
    username = user_entry.get()
    password = pass_entry.get()

    try:
        ssh.connect(hostname, port, username, password)
        output_text.insert(tk.END, f"Connected to {hostname} as {username}.\n")
        # 创建交互式 Shell
        global shell
        shell = ssh.invoke_shell()
        # 启动线程读取远程输出
        threading.Thread(target=read_output, daemon=True).start()
        connect_button.config(state=tk.DISABLED)  # 禁用连接按钮
    except Exception as e:
        messagebox.showerror("Connection Error", str(e))

# 读取远程输出
def read_output():
    while True:
        if shell.recv_ready():  # 检查是否有数据可读
            data = shell.recv(1024).decode('utf-8', errors='ignore')
            output_text.insert(tk.END, data)
            output_text.see(tk.END)  # 自动滚动到最新输出
        else:
            break

# 发送命令到远程服务器
def send_command(event=None):
    command = command_entry.get()
    if not command:
        return

    # 发送命令
    shell.send(command + "\n")
    command_entry.delete(0, tk.END)  # 清空输入框

# 关闭 SSH 连接
def close_connection():
    ssh.close()
    output_text.insert(tk.END, "\nConnection closed.\n")
    connect_button.config(state=tk.NORMAL)  # 启用连接按钮

# 创建 GUI 窗口
root = tk.Tk()
root.title("SSH Terminal Tool")

# 连接信息输入区域
connection_frame = tk.Frame(root)
connection_frame.pack(padx=10, pady=10)

tk.Label(connection_frame, text="Host:").grid(row=0, column=0, sticky="e")
host_entry = tk.Entry(connection_frame, width=20)
host_entry.grid(row=0, column=1, padx=5, pady=5)

tk.Label(connection_frame, text="Port:").grid(row=0, column=2, sticky="e")
port_entry = tk.Entry(connection_frame, width=10)
port_entry.grid(row=0, column=3, padx=5, pady=5)
port_entry.insert(0, "22")  # 默认 SSH 端口

tk.Label(connection_frame, text="Username:").grid(row=1, column=0, sticky="e")
user_entry = tk.Entry(connection_frame, width=20)
user_entry.grid(row=1, column=1, padx=5, pady=5)

tk.Label(connection_frame, text="Password:").grid(row=1, column=2, sticky="e")
pass_entry = tk.Entry(connection_frame, width=20, show="*")
pass_entry.grid(row=1, column=3, padx=5, pady=5)

connect_button = tk.Button(connection_frame, text="Connect", command=connect_ssh)
connect_button.grid(row=2, column=0, columnspan=4, pady=10)

# 输出显示区域
output_text = scrolledtext.ScrolledText(root, wrap=tk.WORD, width=80, height=20)
output_text.pack(padx=10, pady=10)

# 命令输入框
command_frame = tk.Frame(root)
command_frame.pack(padx=10, pady=5)

tk.Label(command_frame, text="Command:").pack(side=tk.LEFT)
command_entry = tk.Entry(command_frame, width=70)
command_entry.pack(side=tk.LEFT, padx=5)
command_entry.bind("<Return>", send_command)  # 绑定回车键发送命令

# 关闭连接按钮
close_button = tk.Button(root, text="Close Connection", command=close_connection)
close_button.pack(pady=10)

# 运行主循环
root.mainloop()